package com.jxy.homepage.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxy.BBM_info.BBM;
import com.jxy.BBM_info.BBM_table;
import com.jxy.BBM_info.utils.BBM_util;
import com.jxy.homepage.domain.Tree;
import com.jxy.homepage.mapper.TreeMapper;
import com.jxy.homepage.service.TreeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author J
 * @description 针对表【j_tree(展示树)】的数据库操作Service实现
 * @createDate 2022-04-21 14:23:22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TreeServiceImpl extends ServiceImpl<TreeMapper, Tree> implements TreeService {

    private BBM_table bbm;

    @Override
    public BBM doTree(BBM bbm) {
        BBM_table removeTable = bbm.getTable("TREE_REMOVE");
        BBM_table insertTable = bbm.getTable("TREE_INS");
        BBM_table updateTable = bbm.getTable("TREE_UPD");
        try {
            // 删除
            if (null != removeTable) {
                if (removeTable.rowSize() > 0) {
                    Collection<Object> ids = removeTable.getIds();
                    this.removeByIds(ids);
                }
                bbm.getBbmInfo().setCode(1);
            }
            // 新增
            if (insertTable != null) {
                if (insertTable.rowSize() > 0) {
                    this.saveBatch(insertTable.toBean(Tree.class));
                }
                bbm.getBbmInfo().setCode(1);
            }
            // 修改 根据IDS 批量修改
            if (updateTable != null) {
                if (updateTable.rowSize() > 0) {
                    this.updateBatchById(updateTable.toBean(Tree.class));
                }
                bbm.getBbmInfo().setCode(1);
            }
        } catch (Exception e) {
            bbm.getBbmInfo().setCode(-1);
            bbm.getBbmInfo().setMessage(e.getMessage());
        }

        return bbm;
    }

    @Override
    public BBM queryTree(BBM bbm) {
        BBM_table table = bbm.getFirstTable();
        if (table != null) {
            // 查询全部
            if (table.rowSize() <= 0) {
                List<Tree> list = this.list();
                BBM_table table1 = BBM_util.toTable(list, Tree.class);
                bbm.clear();
                bbm.addTable(table1);
                bbm.getBbmInfo().setCode(1);
            } else {
                // 根据条件查询
            }
            return bbm;
        }
        return null;
    }
}




